﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.Studio.Custom;
using DWF.Studio.ISerivces;
using DWF.Studio.Models;
using DWF.Studio.Services;
using Ookii.Dialogs.Wpf;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WpfMessageBoxLibrary;

namespace DWF.Studio.Views
{
    /// <summary>
    /// WindowSetting.xaml 的交互逻辑
    /// </summary>
    public partial class WindowSetting
    {
        private IConfigService _configService = null;

        private IAppService _appService = null;

        public WindowSetting()
        {
            InitializeComponent();

            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                init();
            }
        }

        /// <summary>
        /// 初始化
        /// </summary>
        private void init()
        {
            _appService = AppService.Instance;
            _configService = new ConfigService();
            var sysConfig = _configService.LoadSysConfig();

            //this.tbxPackageUri.Text = sysConfig.PackageUri;
            this.tbxPlatformUri.Text = sysConfig.FlatformUri;
            this.tbxUpgradeUri.Text = sysConfig.UpgradeUri;
            this.tbxWorkSpace.Text = sysConfig.WorkSpace;
            this.tbxAssetsUri.Text = sysConfig.AssetsUri;
            this.cbxAutoSave.IsChecked = sysConfig.IsAutoSave;
            this.cbxLogToFile.IsChecked = sysConfig.IsLogToFile;  //Oliver 2021-06-16 执行日志保存到文件 or not
            this.cbxConsoleOutToFile.IsChecked = sysConfig.IsConsoleOutToFile;  //Oliver 2021-06-17 执行输出保存到文件 or not
            this.cbxSilentRun.IsChecked = sysConfig.IsSilentExecution;  //Oliver 2021-06-24 屏蔽执行输出
            this.cbxDebugShowWindow.IsChecked = sysConfig.IsDebugShowWindow;//Carl 2021-06-28 运行时最小化 命中断点弹出窗口

            var languages = _appService.GetLanguages();
            this.cmbxLanguage.ItemsSource = languages;
            this.cmbxLanguage.SelectedItem = languages.Where(item => item.Id == sysConfig.LangugeId).FirstOrDefault();
            //var isSystemBit = Environment.Is64BitOperatingSystem;
        }

        private void BtnDirectory_Click(object sender, RoutedEventArgs e)
        {
            var sysConfig = _configService.LoadSysConfig();

            VistaFolderBrowserDialog projectDialog = new VistaFolderBrowserDialog();
            projectDialog.SelectedPath = sysConfig.WorkSpace + System.IO.Path.DirectorySeparatorChar.ToString();

            var result = projectDialog.ShowDialog(App.Current.MainWindow);
            if (result == true)
            {
                this.tbxWorkSpace.Text = projectDialog.SelectedPath;
            }
        }

        private void BtnConfirm_Click(object sender, RoutedEventArgs e)
        {
            var sysConfig = _configService.LoadSysConfig();

            //sysConfig.PackageUri = this.tbxPackageUri.Text;
            sysConfig.FlatformUri = this.tbxPlatformUri.Text;
            sysConfig.UpgradeUri = this.tbxUpgradeUri.Text;
            sysConfig.WorkSpace = this.tbxWorkSpace.Text;
            sysConfig.AssetsUri = this.tbxAssetsUri.Text;
            sysConfig.IsAutoSave = cbxAutoSave.IsChecked ?? false;
            sysConfig.IsLogToFile = cbxLogToFile.IsChecked ?? false;  //Oliver 2021-06-16 执行日志保存到文件 or not
            sysConfig.IsConsoleOutToFile = cbxConsoleOutToFile.IsChecked ?? false;  //Oliver 2021-06-16 执行输出保存到文件 or not
            sysConfig.IsSilentExecution = cbxSilentRun.IsChecked ?? false;  //Oliver 2021-06-24 屏蔽执行输出
            sysConfig.IsDebugShowWindow = this.cbxDebugShowWindow.IsChecked ?? false;
            var selectedLanguage = (Language)cmbxLanguage.SelectedItem;
            var isReboot = sysConfig.LangugeId != selectedLanguage.Id;
            sysConfig.LangugeId = selectedLanguage.Id;

            if (sysConfig.IsAutoSave)
            {
                sysConfig.AutoSaveSpan = TimeSpan.FromSeconds(30);
            }
            else
            {
                sysConfig.AutoSaveSpan = null;
            }

            _configService.SaveSysConfig(sysConfig);

            if (isReboot)
            {
                var msgProperties = new WpfMessageBoxProperties()
                {
                    Button = MessageBoxButton.YesNo,
                    ButtonYesText = LocalizationProvider.GetLocalizedString("MessageBox_Button_Yes"),
                    ButtonNoText = LocalizationProvider.GetLocalizedString("MessageBox_Button_No"),
                    Image = MessageBoxImage.Question,
                    Text = LocalizationProvider.GetLocalizedString("WindowSetting_MsgBox_Msg_Restart"),
                    Title = LocalizationProvider.GetLocalizedString("WindowSetting_MsgBox_Caption_Restart"),
                };

                MessageBoxResult dlgResult = WpfMessageBox.Show(this, ref msgProperties);
                if (dlgResult == MessageBoxResult.Yes)
                {
                    _appService.Restart();
                }
            }

            this.Close();
        }

        private void BtnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
